/*
 * @Author: 杜朝腾ZIY00178321 duzhaoteng@jk.com
 * @Date: 2023-04-18 15:59:07
 * @LastEditors: 杜朝腾ZIY00178321 duzhaoteng@jk.com
 * @LastEditTime: 2023-09-09 10:01:53
 * @FilePath: \vite-h5\vite.config.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { resolve } from 'path'
import { defineConfig, type PluginOption } from 'vite'
import Vue from '@vitejs/plugin-vue'
import UnoCSS from 'unocss/vite'
import Inspect from 'vite-plugin-inspect'
import Checker from 'vite-plugin-checker'
// import basicSsl from '@vitejs/plugin-basic-ssl'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import graphql from '@rollup/plugin-graphql'
import { VantResolver } from 'unplugin-vue-components/resolvers'
import { compression } from 'vite-plugin-compression2'
import { visualizer } from 'rollup-plugin-visualizer'
// import ViteCDNPlugin from 'vite-plugin-cdn-import'

export default defineConfig({
  plugins: [
    Vue(),
    UnoCSS(),
    Inspect(),
    Checker({ typescript: false }),
    // 配置https证书
    // basicSsl(),
    AutoImport({
      imports: ['vue', 'vue-router', 'pinia'],
      eslintrc: { enabled: false } // 一旦生成.eslintrc-auto-import.json配置文件之后，最好把enable关掉，否则这个文件每次会在重新加载的时候重新生成，这会导致eslint有时找不到这个文件，当需要更新配置文件的时候再重新打开
    }),
    Components({
      resolvers: [VantResolver()]
    }),
    graphql(),
    compression({
      deleteOriginalAssets: true, // 是否删除原有资产
      threshold: 10240, // 体积大于 threshold 才会被压缩,单位 b，1b=8B, 1B=1024KB  这里相当于 9kb多吧，就会压缩
      algorithm: 'gzip', // 压缩算法,可选 [ 'gzip' , 'brotliCompress' ,'deflate' , 'deflateRaw']
      include: [/\.(js|css)/]
    }),
    visualizer({
      emitFile: false, // 是否生成到打包文件中，false：生成到项目根目录
      filename: 'stats.html', // 分析图生成的文件名
      open: false, // 如果存在本地服务端口，将在打包后自动展示
      template: 'network' // 可选值: treemap, sunburst, network, raw-data, list
    }) as unknown as PluginOption
    // ViteCDNPlugin({
    //   modules: [
    //     {
    //       name: 'lodash',
    //       var: '_',
    //       path: `https://cdn.jsdelivr.net/npm/lodash@4.17.21/lodash.min.js`
    //     }
    //   ]
    // })
  ],
  resolve: {
    alias: {
      '@': resolve(__dirname, './src')
    }
  },
  build: {
    rollupOptions: {
      output: {
        manualChunks: {
          vue: ['vue', 'vue-router', 'pinia', 'pinia-plugin-persist'],
          // axios: ['axios'],
          other: ['unocss', 'vant']
        }
      }
    }
  },
  esbuild: {
    drop: ['console', 'debugger']
  }
})
